package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.FindDetailsXiaoTuActivity;
import com.zzk.lanouhn.myapplication1.modles.FindDetailsModle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by lanouhn on 16/9/8.
 */
public class FindDetailsShiShiAdapter extends RecyclerView.Adapter<FindDetailsShiShiAdapter.MyHolder> {

    private Context context;
    private FindDetailsModle modle;
    private List<FindDetailsModle.TripDaysBean.NodesBean> nodesBeans;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    public FindDetailsShiShiAdapter(Context context, FindDetailsModle modle) {
        this.context = context;
        this.modle = modle;

        nodesBeans = new ArrayList<>();
        for (int i = 0; i < modle.getTrip_days().size(); i++) {
            int day = modle.getTrip_days().get(i).getDay();

            List<FindDetailsModle.TripDaysBean.NodesBean> btemps = modle.getTrip_days().get(i).getNodes();
            for (FindDetailsModle.TripDaysBean.NodesBean b : btemps) {
                b.setTripDay(day);
            }
            nodesBeans.addAll(modle.getTrip_days().get(i).getNodes());
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_find_details, null);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {

        if (position == 0) {
            Picasso.with(context).load(modle.getFront_cover_photo_url()).into(holder.iv_datu);
            Picasso.with(context).load(modle.getUser().getImage()).into(holder.iv_xiaotu);
            holder.rl_zhuti.setText(modle.getName());
            holder.rl_shijian.setText(modle.getStart_date());
            holder.rl_tianshu.setText(modle.getTrip_days().size() + "天");
            holder.rl_tushu.setText(modle.getPhotos_count() + "图");

            holder.rl.setVisibility(View.VISIBLE);
            holder.day.setVisibility(View.GONE);
            holder.ll_weizhi.setVisibility(View.GONE);
            holder.ll_item.setVisibility(View.GONE);

            sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
            editor = sp.edit();

            holder.iv_xiaotu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ID = Integer.toString(modle.getUser().getId());
                    Intent intent = new Intent(context, FindDetailsXiaoTuActivity.class);
                    intent.putExtra("finddetails_id", ID);
                    editor.putString("xiaotu_youji", ID);
                    editor.commit();
                    context.startActivity(intent);
                }
            });


        } else {
            holder.day.setVisibility(View.GONE);
            holder.ll_weizhi.setVisibility(View.VISIBLE);
            holder.ll_item.setVisibility(View.VISIBLE);

            holder.rl.setVisibility(View.GONE);

            int pp = position - 1;

            if (nodesBeans.get(pp).getEntry_name() == null) {
                holder.day.setVisibility(View.VISIBLE);
                holder.ll_weizhi.setVisibility(View.GONE);
                holder.day.setText("DAY" + nodesBeans.get(pp).getTripDay());
            } else {
                holder.day.setVisibility(View.GONE);
                holder.ll_weizhi.setVisibility(View.VISIBLE);
                holder.weizhi.setText(nodesBeans.get(pp).getEntry_name() + "");
            }

            holder.ll_item.removeAllViews();

            for (int z = 0; z < nodesBeans.get(pp).getNotes().size(); z++) {
                View view1 = LayoutInflater.from(context).inflate(R.layout.item_find_details_ll, null);
                TextView ll_tv_duzinr = (TextView) view1.findViewById(R.id.item_find_details_ll_duzinr);
                TextView ll_tv_nr = (TextView) view1.findViewById(R.id.item_find_details_ll_neirong);
                ImageView ll_iv = (ImageView) view1.findViewById(R.id.item_find_details_ll_tupian);

                LinearLayout ll_ll = (LinearLayout) view1.findViewById(R.id.item_find_details_ll_ll);
                TextView ll_weizhi = (TextView) view1.findViewById(R.id.item_find_details_ll_woshiweizhi);

                final TextView pinglun = (TextView) view1.findViewById(R.id.item_find_details_ll_pinlunshu);


                if (nodesBeans.get(pp).getNotes().get(z).getPhoto() == null) {
                    ll_tv_duzinr.setText(nodesBeans.get(pp).getNotes().get(z).getDescription() + "");

                    ll_tv_duzinr.setVisibility(View.VISIBLE);
                    ll_tv_nr.setVisibility(View.GONE);
                    ll_iv.setVisibility(View.GONE);
                } else {
                    if (nodesBeans.get(pp).getNotes().get(z).getDescription() == null) {
                        Picasso.with(context).load(nodesBeans.get(pp).getNotes().get(z).getPhoto().getUrl()).into(ll_iv);

                        ll_iv.setVisibility(View.VISIBLE);
                        ll_tv_duzinr.setVisibility(View.GONE);
                        ll_tv_nr.setVisibility(View.GONE);
                    } else {
                        Picasso.with(context).load(nodesBeans.get(pp).getNotes().get(z).getPhoto().getUrl()).into(ll_iv);

                        ll_tv_nr.setText(nodesBeans.get(pp).getNotes().get(z).getDescription() + "");

                        ll_iv.setVisibility(View.VISIBLE);
                        ll_tv_duzinr.setVisibility(View.GONE);
                        ll_tv_nr.setVisibility(View.VISIBLE);
                    }
                }

                if (nodesBeans.get(pp).getEntry_name() == null) {
                    ll_ll.setVisibility(View.GONE);
                } else {
                    ll_ll.setVisibility(View.VISIBLE);
                    ll_weizhi.setText(nodesBeans.get(pp).getEntry_name() + "");
                }
                //评论
                Random random = new Random();
                final int kk = random.nextInt(50);
                pinglun.setText(kk + "");

                holder.ll_item.addView(view1);

            }
        }


    }


    @Override
    public int getItemCount() {
        int size = 0;
        for (int i = 0; i < modle.getTrip_days().size(); i++) {
            size += modle.getTrip_days().get(i).getNodes().size();
        }

        return size + 1;
    }


    public class MyHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rl;
        private ImageView iv_datu, iv_xiaotu;
        private TextView rl_zhuti, rl_shijian, rl_tianshu, rl_tushu;
        private TextView day, weizhi;

        private LinearLayout ll_item, ll_weizhi;

        public MyHolder(View itemView) {
            super(itemView);
            rl = (RelativeLayout) itemView.findViewById(R.id.item_find_details_rl);
            iv_datu = (ImageView) itemView.findViewById(R.id.item_find_details_rl_datu);
            iv_xiaotu = (ImageView) itemView.findViewById(R.id.item_find_details_rl_xiaotu);
            rl_zhuti = (TextView) itemView.findViewById(R.id.item_find_details_rl_zuti);
            rl_shijian = (TextView) itemView.findViewById(R.id.item_find_details_rl_shijian);
            rl_tianshu = (TextView) itemView.findViewById(R.id.item_find_details_rl_tianshu);
            rl_tushu = (TextView) itemView.findViewById(R.id.item_find_details_rl_tushu);
            day = (TextView) itemView.findViewById(R.id.item_find_details_rl_day);
            weizhi = (TextView) itemView.findViewById(R.id.item_find_details_ll_weizhi_zhuti);
            ll_weizhi = (LinearLayout) itemView.findViewById(R.id.item_find_details_ll_weizhi);
            ll_item = (LinearLayout) itemView.findViewById(R.id.item_find_details_ll_item);
        }
    }
}
